<?php
/**
 * 易换管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Exchangeable;

use App\Http\Controllers\Controller;
use App\Models\yb\ExchangeableModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;


class ExchangeableController extends Controller
{
    use ModelForm;

    protected $header = '易换管理';

    /**
     * 易换列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    public function grid(){

        $grid = new Grid(new ExchangeableModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('id','编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.avatar', '用户头像')->display(function ($filed) {
            return empty($filed) ? config('view.oss_zaiwu') : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('User.nickname','用户昵称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.phone','用户电话')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('content','发布内容')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('created_at','发布时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('User.nickname','用户昵称');
                $filter->like('content','内容');
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('User.phone','用户昵称');
                $filter->between('created_at', '发布时间')->datetime();
            });

            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->disableCreateButton();

        $grid->fixColumns(1, -1);

        $grid->actions(function($actions){
            $actions->disableView();
        });

        $grid->actions(function($actions){
            $actions->disableView();
            $actions->disableEdit();
        });


        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new ExchangeableModel());

        $form->text('content', '商品标题')->rules('required|max:150');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }


}